package com.chandra.pos.order.repository;

import com.chandra.pos.SearchCriteria;
import com.chandra.pos.customer.dao.entity.Customer;
import com.chandra.pos.order.dao.entity.Order;
import com.chandra.pos.order.model.OrderSearchCriteria;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Admin
 * Date: 11/15/13
 * Time: 7:53 PM
 * To change this template use File | Settings | File Templates.
 */

public class OrderRepositoryImpl implements OrderRepositoryCustom {


    @PersistenceContext
    private EntityManager entityManager;


    @Override
    public List<Order> search(OrderSearchCriteria searchCriteria) {


        CriteriaBuilder criteriaBuilder= entityManager.getCriteriaBuilder();
        CriteriaQuery<Order> criteriaQuery=criteriaBuilder.createQuery(Order.class) ;

        Root<Customer> from = criteriaQuery.from(Customer.class);

        List<Predicate> predicates = new ArrayList<Predicate>();


        if (searchCriteria.getDueAmount() != null)
        {

            predicates.add(criteriaBuilder.equal(from.get("dueAmount"), searchCriteria.getDueAmount()));

        }

        if (searchCriteria.getTotalAmount() != null)
            predicates.add(criteriaBuilder.equal(from.get("totalAmount"), searchCriteria.getTotalAmount()));


        if (searchCriteria.getOrderDate() != null)
            predicates.add(criteriaBuilder.equal(from.get("orderDate"), searchCriteria.getOrderDate()));



        if (searchCriteria.getTotalAmount() != null)
            predicates.add(criteriaBuilder.equal(from.get("totalAmount"), searchCriteria.getTotalAmount()));





        if( searchCriteria.getFromOrderDate() !=null &&  searchCriteria.getToOrderDate() !=null)

        {
            List<Predicate> dueDatePredicates = new ArrayList<Predicate>();

            dueDatePredicates.add(criteriaBuilder.greaterThanOrEqualTo(from.<Date>get("dueDate"),
                    searchCriteria.getFromOrderDate()));
            dueDatePredicates.add(criteriaBuilder.lessThanOrEqualTo(from.<Date>get("dueDate"),
                    searchCriteria.getToOrderDate()));

            predicates.add(criteriaBuilder.or(dueDatePredicates.toArray(new Predicate[dueDatePredicates.size()])));

        }

        if( searchCriteria.getFromOrderDate() !=null &&  searchCriteria.getToOrderDate() !=null)

        {
            List<Predicate> orderDatePredicates = new ArrayList<Predicate>();

            orderDatePredicates.add(criteriaBuilder.greaterThanOrEqualTo(from.<Date>get("orderDate"),
                    searchCriteria.getFromOrderDate()));
            orderDatePredicates.add(criteriaBuilder.lessThanOrEqualTo(from.<Date>get("orderDate"),
                    searchCriteria.getToOrderDate()));

            predicates.add(criteriaBuilder.or(orderDatePredicates.toArray(new Predicate[orderDatePredicates.size()])));

        }



        criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
        criteriaQuery.distinct(true);

        return entityManager.createQuery(criteriaQuery).getResultList();


    }
    @Override
    public List<Order> search(SearchCriteria searchCriteria) {


        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Order> criteriaQuery=criteriaBuilder.createQuery(Order.class) ;

        Root<Customer> userRoot = criteriaQuery.from(Customer.class);

        List<Predicate> predicates = new ArrayList<Predicate>();



        for (final Map.Entry<String, String> e : searchCriteria.getFilters().entrySet())
        {

            final String key = e.getKey();
            final String value = e.getValue();

            if ((key != null) && (value != null)) {

                if (value.contains("%")) {
                    predicates.add(criteriaBuilder.like(userRoot.<String> get(key), value));
                } else {
                    predicates.add(criteriaBuilder.equal(userRoot.get(key), value));
                }
            }
        }


        criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
        criteriaQuery.distinct(true);

        return entityManager.createQuery(criteriaQuery).setFirstResult(searchCriteria.getFirst()).setMaxResults(searchCriteria.getPageSize()).getResultList();


    }


}
